<?php

namespace App\Http\Requests\Backend;

use App\Models\Role;
use App\Http\Requests\BaseRequest;

class PermissionRequest extends BaseRequest {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(\Request $request)
    {
        switch ($this->method()) {
            case 'POST':
                $rules = [
                        'name' => 'required|unique:permissions|max:255',
                        'display_name' => 'required|max:255',
                        'group_id' => 'required|exists:permission_groups,id,deleted_at,NULL',
                        'description' => 'sometimes|max:255'
                    ];
                break;
            case 'PUT':
            case 'PATCH':
                $rules = [
                        'name' => "required|unique:permissions,name,{$this->segment(3)}|max:255",
                        'display_name' => 'required|max:255',
                        'group_id' => 'required|exists:permission_groups,id,deleted_at,NULL',
                        'description' => 'sometimes|max:255'
                    ];
                break;
            default:
                $rules = [];
                break;
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'name.required' => '名称不能为空',
            'name.unique' => '名称已存在',
            'name.max' => '名称不能超过 :max 个字',
            'display_name.required' => '别名不能为空',
            'display_name.max' => '别名不能超过 :max 个字',
            'group_id.required' => '权限组是必选项',
            'group_id.exists' => '权限组不存在',
            'description.max' => '描述不能超过 :max 个字',
        ];
    }
}
